<?php
/*
 * 取消订单控制器
 */
namespace Mobile\Controller;

use Mobile\Model\orderModel;
use Mobile\Model\moneylogModel;

use Mobile\Controller\PaywechatController;

class OrdercancelController extends BaseController {

    public $orderModel;
    public $moneylogModel;

    public function __construct() {
        parent::__construct();
        $this->orderModel = new orderModel();
        $this->moneylogModel = new moneylogModel();
    }
    
    /*
     * 取消订单
     */
    protected function _cancel($order,$is_closebysys=0,$close_reason=""){
        //更改订单状态
        if($order['checked'] != 0){
            return false;
        }
        
        //关闭微信支付
//        if($order['pay_money']>0){
//            $wechatpay = new PaywechatController();
//            $out_trade_no = $order['order_sn'];
//            $weres = $wechatpay->closeOrder($out_trade_no);
//            if(true === $weres){
//                //关闭成功
//            }else{
//                //关闭失败
//                return false;
//            }
//        }
        
        $data = array(
            "close_time" => date("Y-m-d H:i:s"),
            "checked" => 6,
            "is_closebysys" => $is_closebysys,
            "close_reason" => $close_reason
        );
        $where = array(
            "order_id" => $order['order_id'],
            "checked" => 0
        );
        $res = $this->orderModel->editOrder($where, $data);
        if(!$res){
            return false;
        }

        $this->_inMoneylog($order['yongjin_money'],$order,1);//返佣金
        
        $this->_inMoneylog($order['cash_money'],$order,3);//返现金
        
        $this->_inMoneylog($order['score_money'],$order,2);//返重消币

        $this->_inMoneylog($order['tihuo_money'],$order,11);//返提货币

        $this->_inMoneylog($order['prize_money'],$order,12);//返提货币
        
        return true;
    }
    
    
    
    /*
     * 取消订单加钱流水,并返回用户表里的钱
     * $mtype 1分销佣金,2重消币 ,3现金电子币 ,4重消佣金,5互助奖,6车房奖, 7领导奖, 8分红奖 ,9保证金 , 10个税 , 11提货币流水
     */
    protected function _inMoneylog($money,$order,$mtype){
    
        if($money <= 0){
            return false;//金额等于0 不做任何操作
        }
    
        $uid = $order['uid'];
    
        switch($mtype){
            case 2:
                $this->memberModel->jiaQian($uid,'score',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('score')->find();
                $yue = $userinfo['score'];
                break;
            case 3:
                $this->memberModel->jiaQian($uid,'cash_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('cash_money')->find();
                $yue = $userinfo['cash_money'];
                break;
            case 1:
                $this->memberModel->jiaQian($uid,'money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('money')->find();
                $yue = $userinfo['money'];
                break;
            case 11:
                $this->memberModel->jiaQian($uid,'tihuo_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('tihuo_money')->find();
                $yue = $userinfo['tihuo_money'];
                break;
            case 12:
                $this->memberModel->jiaQian($uid,'prize_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('prize_money')->find();
                $yue = $userinfo['prize_money'];
                break;
            default:
                return false;
        }

        $money_log = array(
            "uid" => $order['uid'],
            "nickname" => $order['realname'],
            "logtype" => 0,//收入 1收入 0支出
            "mtype" => $mtype,//类型
            "money" => $money,
            "content" => "【订单取消】".$order['order_title'],
            "order_id" => $order['order_id'],
            "order_uid" => $order['uid'],
            "order_paymoney" => $order['order_cashmoney'],//订单支付的金额(需要分佣的金额)
            "order_nickname" => $order['nickname'],
            "create_time" => date("Y-m-d H:i:s"),//写流水的时间
            "un_lock" => 1,//不锁定,立即扣除 1解锁,0锁定
            "cash_money" => $yue,//查余额
            "note" => "取消订单"
        );
    
    
        $res = $this->moneylogModel->addMoneylog($money_log);
    
        return $res;
    
    }
    
    
    
    
    
    
    
    
    /*
     * 定时任务,1个小时后自动关闭订单
     */
    public function croncloseorder(){
        
        //查找出未付款的  满了指定时间的订单
        $min = C("CLOSE_ORDER_TIME");
        $min = intval($min);
        if($min<30 || $min>4320){
            $min = 60;
        }
        
        
        $time = date("Y-m-d H:i:s",time()-60*$min);
        $where = array(
            "checked" => 0,
            "create_time" => array("lt",$time),
        );
        $res = $this->orderModel->getOrderLists($where);//取出全部订单
        if(empty($res)){
            exit("no order");
        }
        
        foreach($res as $k=>$v){
            //如果仅仅只使用了微信支付,不关闭订单
            if($v['yongjin_money'] > 0 || ($v['cash_money'] > 0) || $v['score_money']>0){
                $this->_cancel($v,1,"");
            }
        }
        
        echo "ok";exit;
    }
    
    
    
    /*
     * 用户手动取消订单
     */
    public function index(){
        
        $order_id = get('order_id');
        if(!$order_id){
            return_json(1,"订单参数错误");
        }
        
        $where = array(
            "order_id" => $order_id,
            "uid" => $this->uid
        );
        //查找订单
        $order = $this->orderModel->getOrder($where);
        if(empty($order)){
            return_json(1,"订单不存在,请检查");
        }
        
        if($order['checked'] != 0){
            return_json(1,"当前订单状态不允许取消订单");
        }
        
        if($order['create_time'] > date("Y-m-d H:i:s",time()-1*60)){
            return_json(1,"为了你的账户安全,订单创建1分钟内不允许取消,请1分钟后重试");
        }
        
        $close_reason = get("close_reason");
        
        $res = $this->_cancel($order,$is_closebysys=0,$close_reason);
        
        if($res){
            return_json(0,"订单已取消","","/mobile/orderdetail?order_id=".$order_id);
        }
        
        return_json(1,"订单取消失败");
        
    }

}